/**
 * 
 */
package com.hsxc.cdz.controller.wxController.util;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.hsxc.cdz.controller.wxController.entity.AcessToken;
import com.hsxc.cdz.controller.wxController.entity.JsapiTicket;
import com.hsxc.cdz.controller.wxController.entity.Openid;
import com.hsxc.cdz.controller.wxController.jssdk.Jssdk;
import com.hsxc.cdz.controller.wxController.entity.Openid;

/**
 * @author wpfei
 * @version 创建时间：2016年6月27日 上午11:46:34 类说明 ：
 */

public class Getvar {
	
	protected static Logger log = Logger.getLogger("Getvar");;

	public static AcessToken getJSSDKAccessToken() {
		System.out.println("进入获取Accesstoken");
		String requestUrl = Constants.weixin_jssdk_acceToken_url;
		JSONObject datajson=null;
		AcessToken acesstoken=null;
	
		try {
			String output = new String(HttpSLL.getMethod(requestUrl));
			 datajson = new JSONObject(output);
			System.out.println(output);
		if (null != datajson) {
			 acesstoken = new AcessToken();
			 acesstoken.setAccessToken(datajson.getString("access_token"));
			 acesstoken.setExpiresIn(datajson.getInt("expires_in"));
            } 
		}catch (JSONException e) {
			
                acesstoken = null;
                // 获取token失败
                try {
					log.error("获取token失败 errcode:{} errmsg:{}"+ datajson.getInt("errcode")+ datajson.getString("errmsg"));
				} catch (JSONException e1) {
					
					e1.printStackTrace();
				}
            }
		return acesstoken;
        }	

	public static JsapiTicket getJSSDKTicket() {
		PropertiesUtil putil=new PropertiesUtil(Constants.CONF_PROPERTIES_PATH);
		String accessToken = putil.getProperty("ACCESS_TOKEN");
		System.out.println("accessToken是"+accessToken);
		String requestUrl = Constants.weixin_jssdk_ticket_url.replace("ACCESS_TOKEN",accessToken);
		JSONObject datajson=null;
		JsapiTicket ticket=null;
		try {
			String output = new String(HttpSLL.getMethod(requestUrl));
			 datajson = new JSONObject(output);
			System.out.println(output);
		if (null != datajson) {
			ticket = new JsapiTicket();
			ticket.setTicket(datajson.getString("ticket"));
			ticket.setExpires_in(datajson.getInt("expires_in"));
            } 
		}catch (JSONException e) {
			
                ticket = null;
                // 获取token失败
                try {
					log.error("获取ticket失败 errcode:{} errmsg:{}"+ datajson.getInt("errcode")+ datajson.getString("errmsg"));
				} catch (JSONException e1) {
					
					e1.printStackTrace();
				}
            }

		return ticket;
	}

	public static String getCode() {

		return Jssdk.getCode();

	}

	public static Openid getOpenid() {
		String temp = getCode();
		JSONObject datajson=null;
		Openid openid=null;
		if(!temp.isEmpty())
		{
		String requestUrl =Constants.weixin_jssdk_openid_url.replace("CODE", getCode());
		try {
			String output = new String(HttpSLL.getMethod(requestUrl));
			System.out.println("--------putput----------"+output);
			 datajson = new JSONObject(output);
			System.out.println(output);
		if (null != datajson) {
			openid = new Openid();
			openid.setAccess_token(datajson.getString("access_token"));
		    openid.setExpires_in(datajson.getInt("expires_in"));
		    openid.setRefresh_token(datajson.getString("refresh_token"));
		    openid.setScope(datajson.getString("scope"));
		    openid.setOpenid(datajson.getString("openid"));
			
            } 
		}catch (JSONException e) {
			
                openid = null;
                try {
					log.error("获取openid失败 errcode:{} errmsg:{}"+ datajson.getInt("errcode")+ datajson.getString("errmsg"));
				} catch (JSONException e1) {
					
					e1.printStackTrace();
				}
		}
    
	}else{
		log.error("code值为空，请点击充电按钮");
		openid = null;
	}
		return openid;

	}
}
	 
